<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 5000px;
        }
        
        div {
            width: 100px;
            height: 100px;
            background-color: aqua;
            display: none;
            position: fixed;
            bottom: 200px;
            right: 100px;
        }
    </style>
</head>

<body>
    <div>回到顶部</div>
    <script>
        let div = document.querySelector("div")
        window.addEventListener("scroll", function() {
            console.log(scrollY);
            if (scrollY > 800) {
                div.style.display = "block"
            } else {
                div.style.display = "none"
            }
        })
        div.addEventListener("click", function() {
            let a = setInterval(() => {
                scroll(0, scrollY - 200)
                if (scrollY == 0) {
                    clearInterval(a)
                }
            }, 50);
        })
    </script>
</body>

</html>